package com.shaoshaohuo.app.adapter;

import java.util.List;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.CarOption;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.Order;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.utils.StringHelper;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class OrderListAdapter<T> extends MyBaseAdapter<Order> {

	private static final int AGREE = 1;
	private static final int REFUSE = 2;
	
	private int type = 1;

	public OrderListAdapter(Context ctx, List<Order> list, boolean flag) {
		super(ctx, list, flag);
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup group) {
		
		ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context,R.layout.view_order_list_item, null);
            holder = new ViewHolder();
            holder.startCityText = (TextView) convertView.findViewById(R.id.textview_start_city);
            holder.endCityText = (TextView) convertView.findViewById(R.id.textview_end_city);
            holder.priceText = (TextView) convertView.findViewById(R.id.textview_money);
            holder.weightText = (TextView) convertView.findViewById(R.id.textview_weight);
            holder.startAddressText = (TextView) convertView.findViewById(R.id.textview_start_address);
            holder.endAddressText = (TextView) convertView.findViewById(R.id.textview_end_address);
            holder.timeText = (TextView) convertView.findViewById(R.id.textview_time);
            holder.statusText = (TextView) convertView.findViewById(R.id.textview_status);
            holder.headerLayout = convertView.findViewById(R.id.layout_header);
            holder.carUserText = (TextView) convertView.findViewById(R.id.textview_car_user);
            holder.orderIdText = (TextView) convertView.findViewById(R.id.textview_order_id);
            holder.mExcuteLayout = convertView.findViewById(R.id.layout_excute);
            holder.mAgreeText = (TextView) convertView.findViewById(R.id.textview_jiedan);
            holder.refuseText = (TextView) convertView.findViewById(R.id.textview_jujue);
            
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
		
        final Order item = list.get(pos);
        
        holder.startCityText.setText(item.getOrigincityname());
        holder.endCityText.setText(item.getReceivecityname());
        holder.priceText.setText("￥"+item.getMoney());
//        holder.weightText.setText(item.getShipptype()+"/"+item.getShippweight());
        holder.startAddressText.setText(item.getOriginaddress());
        holder.endAddressText.setText(item.getReceiveaddress());
        holder.timeText.setText("下单时间:"+StringHelper.formatDateMinute(item.getCursor()));
        holder.statusText.setText(getStatusText(item.getStatus(), item.getCaruserstatus(), item.getShipperstatus(), item.getCarerdiscuss(),holder));
        
        holder.carUserText.setText("货主:"+item.getShipperuserinfo().getRealname());
        holder.orderIdText.setText("订单编号:"+item.getOrderid());
        
        if ("1".equals(item.getShipptype())) {
        	holder.weightText.setText(CarOption.cargoItems[0]);
        } else if ("2".equals(item.getShipptype())) {
        	
        	double parseDouble = Double.parseDouble(item.getShippweight());
        	if(parseDouble>1000){
        		holder.weightText.setText(CarOption.cargoItems[1] + "/" + (parseDouble/1000) + "吨");
        	}else{
        		holder.weightText.setText(CarOption.cargoItems[1] + "/" + item.getShippweight() + "kg");
        	}
        	
        } else if ("3".equals(item.getShipptype())) {
        	holder.weightText.setText(CarOption.cargoItems[2] /*+ "/" + item.getShipparea() + "m³"*/);
        }else{
        	holder.weightText.setVisibility(View.GONE);
        }
        
        switch (type) {
		case 1:
			holder.orderIdText.setVisibility(View.GONE);
			break;
		case 2:
			holder.orderIdText.setVisibility(View.GONE);
			break;
		case 3:
			holder.orderIdText.setVisibility(View.VISIBLE);
			break;
		}
        
        holder.mAgreeText.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showAgreeRefuseDialog("确定接单吗？", AGREE,item);
			}
		});
        
        holder.refuseText.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showAgreeRefuseDialog("确定拒绝此订单吗？", REFUSE,item);
			}
		});
        
		return convertView;
	}
	
	
	
	private String getStatusText(String status,String carStatus,String userStatus,String carerdiscuss,ViewHolder holder) {
		holder.mExcuteLayout.setVisibility(View.GONE);
		holder.statusText.setVisibility(View.VISIBLE);
		switch (Integer.parseInt(status)) {
		case -4:
		case -3:
		case -2:
		case -1:
			return "订单失效";
		case 1:
			if("2".equals(userStatus)){
				return "等待货主同意";
			}else if("3".equals(userStatus)){
				return "等待货主支付";
			}
			break;
		case 2:
			if("1".equals(carStatus) && "3".equals(userStatus)){
				
				holder.mExcuteLayout.setVisibility(View.VISIBLE);
				holder.statusText.setVisibility(View.GONE);
				return "接单 拒绝";
			}
			break;
		case 3:
			if("3".equals(userStatus)){
				return "等待货主支付";
			}
		case 4:
			if("3".equals(userStatus)){
				return "等待货主支付";
			}
			break;
		case 5:
			return "待装货";
		case 6:
			return "运输中";
		case 7:
			if("1".equals(carerdiscuss)){
				return "已完成,未评价";
			}else{
				return "已完成,已评价";
			}
		default:
			return "";
		}
		return "";
	}
	
	static class ViewHolder{
		TextView startCityText;
		TextView endCityText;
		TextView priceText;
		TextView weightText;
		
		TextView startAddressText;
		TextView endAddressText;
		TextView timeText;
		TextView statusText;
		
		View headerLayout;
		TextView carUserText;
		TextView orderIdText;
		
		View mExcuteLayout;
		TextView mAgreeText;
		TextView refuseText;
	}
	
	private void showAgreeRefuseDialog(String message, final int typeid,final Order item) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(message);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				excuteOrder(typeid,item);
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		builder.create().show();
	}
	
	private void excuteOrder(final int typeid,final Order item) {
		((BaseActivity)context).startLoadingDialog();
		RequestService.getInstance().driverOperate(context, item.getOrderid(), typeid + "", BaseEntity.class, new RequestListener() {

			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				((BaseActivity)context).dismissLoadingDialog();
				if (resultData.isOk()) {
					if (typeid == AGREE) {
						((BaseActivity)context).showToast("接单成功");
					} else {
						((BaseActivity)context).showToast("拒绝成功");
					}
					list.remove(item);
					notifyDataSetChanged();
//					requestData();
				} else {
					((BaseActivity)context).showToast(resultData.getMsg());
				}
//				requestData();
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				((BaseActivity)context).dismissLoadingDialog();
				((BaseActivity)context).showToast(R.string.please_check_network);
			}
		});
	}
	

	public void setType(int i) {
		this.type = i;
	}
	
}
